<!-- 销售类型关联在途状态 -->
<template>
  <div class="mod-config">
      <el-button-group style="margin-bottom: 10px;">
        <el-button type="primary" icon="el-icon-search" @click="searchChange">查询</el-button>
        <el-button type="primary" icon="el-icon-delete" @click="searchReset">清空</el-button>
        <el-button v-if="permissions.eims_saletypeintransit_add" icon="el-icon-plus" type="primary" @click="addOrUpdateHandle()">新增</el-button>
      </el-button-group>
      <el-form inline ref="searchForm" :model="dataForm" @keyup.enter.native="searchChange()">
        <el-form-item label="销售单类型" prop="saleType">
          <el-select v-model="dataForm.saleType" placeholder="配镜类型">
            <el-option v-for="item in saleTypeOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div class="avue-crud">
        <el-table :data="dataList" border v-loading="dataListLoading" :max-height="tableHeight">
          <el-table-column prop="saleType" label="销售单类型" width="130">
            <template slot-scope="scope">
              {{valueOfName(scope.row.saleType,saleTypeOptions)}}
            </template>
          </el-table-column>
          <el-table-column prop="inTransitNames" label="在途状态"></el-table-column>
          <el-table-column label="操作" width="140">
            <template slot-scope="scope">
              <el-button v-if="permissions.eims_saletypeintransit_edit" type="text" icon="el-icon-edit" @click="addOrUpdateHandle(scope.row.saleType)">修改</el-button>
              <el-button v-if="permissions.eims_saletypeintransit_del" type="text" icon="el-icon-delete" @click="deleteHandle(scope.row.saleType)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="avue-crud__pagination">
        <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
          :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" background layout="total, sizes, prev, pager, next">
        </el-pagination>
      </div>
      <!-- 弹窗, 新增 / 修改 -->
      <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></table-form>
  </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { fetchEimsSaleTypeInTransitList, delEimsSaleTypeInTransitObj } from '@/api/eims/saletypeintransit'
  import { getSaleStatus } from "@/api/eims/optician/glassesinfo"

  import TableForm from './eimssaletypeintransit-form'

  export default {
    data() {
      return {
        dataForm: {
          key: ''
        },
        saleTypeOptions: [
          { value: 1, label: '隐形订做' },
          { value: 2, label: '框镜订做' },
          { value: 3, label: '隐形成品' },
          { value: 4, label: '框镜成品' },
          { value: 5, label: '辅料' }
        ],
        saleStatusOption: [],
        dataList: [],
        searchShow: true,
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        addOrUpdateVisible: false
      }
    },
    components: {
      TableForm
    },
    created() {
      this.getDataList()
    },
    computed: {
      ...mapGetters(['permissions']),
      tableHeight: function(){
        return window.innerHeight - 260
      }
    },
    methods: {
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true

        fetchEimsSaleTypeInTransitList(Object.assign(this.dataForm,{
          current: this.pageIndex,
          size: this.pageSize
        })).then(response => {
          if(response.data.code == '0'){
            this.dataList = response.data.data.records
            this.totalPage = response.data.data.total
          }else{
            this.$message.error(response.data.msg)
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 新增 / 修改
      addOrUpdateHandle(type) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(type)
        })
      },
      // 删除
      deleteHandle(type) {
        this.$confirm('是否确认删除', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delEimsSaleTypeInTransitObj(type)
        }).then(response => {
          if(response.data.code == '0'){
            this.$message.success('删除成功')
            this.getDataList()
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      searchChange() {
        this.pageIndex = 1;
        this.getDataList()
      },
      // 搜索清空
      searchReset() {
        this.$refs.searchForm.resetFields();
        this.getDataList(this.page)
      },
      handleSearchShow() {
        this.searchShow = !this.searchShow;
      },
      getOpticianStatus(){
        this.saleStatusOption = result.data.data
      },
      valueOfName(value,option){
        let obj = {}
        obj = option.find(item => {
          if(value == item.value){
            return item
          }
        })
        if(obj && obj.label){
          return obj.label
        }
      },
      statusOfName(value){
        let list = []
      //   value.forEach(item => {
      //     list.push(item.name)
      //   })
      //   return list.join(',')
      },
      // 刷新回调
      refreshChange() {
        this.getDataList(this.page)
      }
    }
  }
</script>
